const DATA_TYPE = {
    ALL     :   0,
    GROUP   :   1,
    CHANNEL :   2
}

const FUNC_URL = {
    RES_ALL         :       '/ivms/res/all',
    RES_GROUP       :       '/ivms/res/group',
    RES_CHANNEL     :       '/ivms/res/channel'
}

class iVMS {
    constructor ( { server, authorization } ) {

        this.event = {
            error: () => {}
        }

        this.server = server
        this.authorization = authorization

        this.groupList = null
        this.channelList = null

    }

    /**
     * 查询数据集合
     * @param type  数据类型，参考DATA_TYPE
     * @param page  查询页码，默认为0查询全部，每页为100条记录
     */
    list ( type, page = 0 ) {
        let url = `http://${this.ip}:${this.port}`
        switch (type) {
            case DATA_TYPE.GROUP:
                url += FUNC_URL.RES_GROUP
                break
            case DATA_TYPE.CHANNEL:
                url += FUNC_URL.RES_CHANNEL
                break;
            default:
                url += FUNC_URL.RES_ALL
                break
        }
        axios.get(url, {
            params: {
                PageNum: page,
                AuthorizationId: this.authorization
            }
        }).then(resp => {
            this.channelList = resp.data['Resource']
            return this.channelList
        }).catch(err => {
            this.emit('error', err)
        })
    }

    data ( channel ) {

    }

    isAlive ( channel ) {

    }

    on (event, callback) {
        this.event[event] = callback
    }

    emit (event, data) {
        try {
            this.event[event].call(null, data)
        } catch (e) {
            console.error(e)
        }
    }

}
